/*!
    \file    gd32f30x_it.c
    \brief   interrupt service routines

    \version 2017-02-10, V1.0.0, firmware for GD32F30x
    \version 2018-10-10, V1.1.0, firmware for GD32F30x
    \version 2018-12-25, V2.0.0, firmware for GD32F30x
    \version 2020-09-30, V2.1.0, firmware for GD32F30x
*/

/*
    Copyright (c) 2020, GigaDevice Semiconductor Inc.

    Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
*/

#include <stdio.h>
#include <string.h>
#include "gd32f30x.h"
#include "gd32f30x_it.h"
#include "systick.h"
#include "key_inpu.h"
#include "lvd.h"
#ifdef ENABLE_I2C_TEST
#include "i2c.h"
#endif
#include "usart.h"
#ifdef ENABLE_SPI_TEST
#include "spi.h"
#endif
#ifdef ENABLE_FREERTOS
#include "FreeRTOS.h"
#include "task.h"
#endif

/*!
    \brief      this function handles NMI exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void NMI_Handler(void)
{
}

/*!
    \brief      this function handles HardFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void HardFault_Handler(void)
{
    /* if Hard Fault exception occurs, go to infinite loop */
    while (1){
    }
}

/*!
    \brief      this function handles MemManage exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void MemManage_Handler(void)
{
    /* if Memory Manage exception occurs, go to infinite loop */
    while (1){
    }
}

/*!
    \brief      this function handles BusFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void BusFault_Handler(void)
{
    /* if Bus Fault exception occurs, go to infinite loop */
    while (1){
    }
}

/*!
    \brief      this function handles UsageFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void UsageFault_Handler(void)
{
    /* if Usage Fault exception occurs, go to infinite loop */
    while (1){
    }
}

#ifndef ENABLE_FREERTOS
/*!
    \brief      this function handles SVC exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void SVC_Handler(void)
{
}
#endif

/*!
    \brief      this function handles DebugMon exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void DebugMon_Handler(void)
{
}

#ifndef ENABLE_FREERTOS
/*!
    \brief      this function handles PendSV exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void PendSV_Handler(void)
{
}
#endif

#ifdef ENABLE_FREERTOS
extern void xPortSysTickHandler(void);
#endif
/*!
    \brief      this function handles SysTick exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
#ifdef SYSTICK_INTERRUPT_MODE
void SysTick_Handler(void)
{
#ifdef ENABLE_FREERTOS

    if(xTaskGetSchedulerState()!=taskSCHEDULER_NOT_STARTED)//系统已经运行
    {
       xPortSysTickHandler();
    }
    else
    {
        delay_decrement();
    } 

#else
    delay_decrement();

#endif
}
#endif

uint8_t  LedFlag;
void TIMER1_IRQHandler(void)
{
	timer_flag_clear(TIMER1,TIMER_FLAG_UP);
    LedFlag^=0x01;
    if(LedFlag)//计数到500ms,翻转输出
	{
		gpio_bit_set(GPIOB,GPIO_PIN_4);//PB4输出高电平
		
	}else
	{
		gpio_bit_reset(GPIOB,GPIO_PIN_4);//PB4输出低电平
	}
}

static int timer2_num = 0;
void TIMER2_IRQHandler(void)
{
	timer_flag_clear(TIMER2,TIMER_FLAG_UP);
    if (timer2_num == 1000)
    {   
        timer_disable(TIMER2);
        printf("timer2 disable\r\n");
        timer2_num = 0;
    }
    timer2_num++;
}


void EXTI1_IRQHandler(void)
{
    exti_interrupt_flag_clear(EXTI_1);
    
#if ENABLE_KEY_INPUT_TEST == 1    
    delay_1ms(20);
    if (gpio_input_bit_get(GPIOA,GPIO_PIN_1) == RESET)
    {
        printf("User Key Press and Enter Standy Mode!\r\n");
        pmu_to_standbymode();//进入待机模式
    }
#endif  //#if ENABLE_KEY_INPUT_TEST == 1

    if (gpio_input_bit_get(GPIOB,GPIO_PIN_1) == RESET)
    {
        hander_message();
    }
}


#if ENABLE_LVD_TEST == 1
/*!
    \brief      this function handles LVD exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void LVD_IRQHandler(void)
{
    if(RESET != exti_interrupt_flag_get(EXTI_16)){
        
        printf("detect muc is low voltage\r\n");
    }
}
#endif //#if ENABLE_LVD_TEST == 1

#ifdef ENABLE_RTC_TEST  
extern  uint32_t timedisplay;
void RTC_IRQHandler(void)
{
    if (rtc_flag_get(RTC_FLAG_SECOND) != RESET){
        /* clear the RTC second interrupt flag*/
        rtc_flag_clear(RTC_FLAG_SECOND);

        /* enable time update */
        timedisplay = 1;

        /* wait until last write operation on RTC registers has finished */
        rtc_lwoff_wait();
        /* reset RTC counter when time is 23:59:59 */
        if (rtc_counter_get() == 0x00015180){
            rtc_counter_set(0x0);
            /* wait until last write operation on RTC registers has finished */
            rtc_lwoff_wait();
        }
    }
}
#endif

#ifdef ENABLE_DMA_TEST
extern FlagStatus g_transfer_complete;
void DMA0_Channel3_IRQHandler(void)
{
    if(dma_interrupt_flag_get(DMA0, DMA_CH3, DMA_INT_FLAG_FTF)){     
        dma_interrupt_flag_clear(DMA0, DMA_CH3, DMA_INT_FLAG_G);
        g_transfer_complete = SET;
    }
}
#endif //ENABLE_DMA_TEST

#ifdef ENABLE_USART1_TEST

void USART1_IRQHandler(void)
{
    if(RESET != usart_interrupt_flag_get(USART1, USART_INT_FLAG_IDLE)){
        
        /* clear IDLE flag */
        usart_data_receive(USART1);
        /* number of data received */
        usart1_rxcount = 256 - (dma_transfer_number_get(DMA0, DMA_CH5));

        if (usart1_rxcount >0)
        {
            handle_usart2_message(usart1_rx_buffer,usart1_rxcount);
            memset(usart1_rx_buffer,0,sizeof(usart1_rx_buffer));
        }
        usart_interrupt_flag_clear(USART1,USART_INT_FLAG_IDLE);
      
        /* disable DMA and reconfigure */
        dma_channel_disable(DMA0, DMA_CH5);
        dma_transfer_number_config(DMA0, DMA_CH5, 256);
        dma_channel_enable(DMA0, DMA_CH5);
    }
}
#endif

#ifdef ENABLE_I2C_TEST

#if I2C1_MODE == I2C1_MODE_SLAVE
/*!
    \brief      I2C1 event handle function
    \param[in]  none
    \param[out] none
    \retval     none
*/
void I2C1_EV_IRQHandler(void)
{
    I2C1_EventIRQ_Handler();
}

/*!
    \brief      I2C1 error handle function
    \param[in]  none
    \param[out] none
    \retval     none
*/
void I2C1_ER_IRQHandler(void)
{
    I2C1_ErrorIRQ_Handler();
}
#endif
#endif

#if SPI_MODE == SPI_MASTER_SLAVE_MODE_INT
uint8_t spi1_receive_array[128] = {0};
uint8_t receive_n = 0;
uint8_t slave_tx_buf[16] = {0x90,0x98,0x99,0x10,0x12,0x4,0x5,0x6,0x7,0x8,0x3,0x2,0x4,0x5,0x1,0x3};
uint8_t tx_n = 0;


void SPI1_IRQHandler(void)
{
    /* receive data */
    if(RESET != spi_i2s_interrupt_flag_get(SPI1,SPI_I2S_INT_FLAG_RBNE)) 
    {
        spi1_receive_array[receive_n++] = spi_i2s_data_receive(SPI1);

        // while(RESET == spi_i2s_flag_get(SPI1, SPI_FLAG_TBE));
	    // spi_i2s_data_transmit(SPI1, slave_tx_buf[tx_n++]);
        if (receive_n == 16)
        {  
            //handle_spi1_message(spi1_receive_array,16);
            receive_n = 0;
        }
    }
}
#endif